package com.yoyo.series.data;

import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.yoyo.series.MyApplication;
import com.yoyo.series.entities.CategoryEntity;
import com.yoyo.series.entities.ImageEntity;
import com.yoyo.series.entities.ModuleEntity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Repository {

    private static Repository repository;
    private SQLiteDatabase db;

    private Repository() {
        db = (new DBHelper(MyApplication.gAppContext)).getWritableDatabase();
    }

    public static Repository getInstance() {
        if (repository == null)
            repository = new Repository();
        return repository;
    }

    public void add(CategoryEntity categoryentity) {
        try {
            db.beginTransaction();
            ContentValues contentvalues = new ContentValues();
            contentvalues.put("groupId", Integer.valueOf(categoryentity.groupId));
            contentvalues.put("categroyId", Long.valueOf(categoryentity.categroyId));
            contentvalues.put("description", categoryentity.description);
            contentvalues.put("iconUrl", categoryentity.iconUrl);
            contentvalues.put("timestamp", Long.valueOf(categoryentity.timestamp));
            contentvalues.put("count", Integer.valueOf(categoryentity.count));
            contentvalues.put("isFav", Integer.valueOf(categoryentity.isFav));
            db.replace("collection", null, contentvalues);
            db.setTransactionSuccessful();
            db.endTransaction();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void add(ModuleEntity moduleentity) {
        try {
            db.beginTransaction();
            ContentValues contentvalues = new ContentValues();
            contentvalues.put("groupId", Integer.valueOf(moduleentity.groupId));
            contentvalues.put("moduleName", moduleentity.moduleName);
            contentvalues.put("iconUrl", moduleentity.iconUrl);
            db.replace("module", null, contentvalues);
            db.setTransactionSuccessful();
            db.endTransaction();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void addCategroy(List<CategoryEntity> list) {
        try {
            db.beginTransaction();
            Iterator<CategoryEntity> iterator = list.iterator();
            ContentValues contentvalues = null;
            CategoryEntity categoryentity;
            ContentValues contentvalues1;
            while (iterator.hasNext()) {
                categoryentity = iterator.next();
                contentvalues1 = new ContentValues();
                contentvalues1.put("groupId", Integer.valueOf(categoryentity.groupId));
                contentvalues1.put("categroyId", Long.valueOf(categoryentity.categroyId));
                contentvalues1.put("description", categoryentity.description);
                contentvalues1.put("iconUrl", categoryentity.iconUrl);
                contentvalues1.put("timestamp", Long.valueOf(categoryentity.timestamp));
                contentvalues1.put("count", Integer.valueOf(categoryentity.count));
                contentvalues1.put("isFav", Integer.valueOf(categoryentity.isFav));
                db.replace("collection", null, contentvalues1);
                contentvalues = contentvalues1;
            }
            db.setTransactionSuccessful();
            db.endTransaction();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void addImage(List<ImageEntity> list) {
        try {
            db.beginTransaction();
            Iterator<ImageEntity> iterator = list.iterator();
            ContentValues contentvalues = null;
            ImageEntity imageentity;
            ContentValues contentvalues1;
            while (iterator.hasNext()) {
                imageentity = ( ImageEntity ) iterator.next();
                contentvalues1 = new ContentValues();
                contentvalues1.put("groupId", Integer.valueOf(imageentity.groupId));
                contentvalues1.put("categroyId", Long.valueOf(imageentity.categroyId));
                contentvalues1.put("imageId", Long.valueOf(imageentity.imageId));
                contentvalues1.put("description", imageentity.description);
                contentvalues1.put("imageUrl", imageentity.imageUrl);
                contentvalues1.put("timestamp", Long.valueOf(imageentity.timestamp));
                db.replace("image", null, contentvalues1);
                contentvalues = contentvalues1;
            }
            db.setTransactionSuccessful();
            db.endTransaction();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void addModules(List list) {
        try {
            db.beginTransaction();
            db.delete("module", null, null);
            ContentValues contentvalues;
            for (Iterator iterator = list.iterator(); iterator.hasNext(); db.replace("module", null,
                    contentvalues)) {
                ModuleEntity moduleentity = ( ModuleEntity ) iterator.next();
                contentvalues = new ContentValues();
                contentvalues.put("groupId", Integer.valueOf(moduleentity.groupId));
                contentvalues.put("moduleName", moduleentity.moduleName);
                contentvalues.put("iconUrl", moduleentity.iconUrl);
            }
            db.setTransactionSuccessful();
            db.endTransaction();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void addToFav(int i, long l) {
        try {
            ContentValues contentvalues = new ContentValues();
            contentvalues.put("isFav", Integer.valueOf(1));
            SQLiteDatabase sqlitedatabase = db;
            String as[] = new String[2];
            as[0] = (new StringBuilder()).append(i).append("").toString();
            as[1] = (new StringBuilder()).append(l).append("").toString();
            sqlitedatabase.update("collection", contentvalues, "groupId=? and categroyId=?", as);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void cancelFav(int i, long l) {
        SQLiteDatabase sqlitedatabase = db;
        String as[] = new String[2];
        as[0] = (new StringBuilder()).append(i).append("").toString();
        as[1] = (new StringBuilder()).append(l).append("").toString();
        sqlitedatabase.delete("collection", "groupId=? and categroyId=?", as);
        SQLiteDatabase sqlitedatabase1 = db;
        String as1[] = new String[2];
        as1[0] = (new StringBuilder()).append(i).append("").toString();
        as1[1] = (new StringBuilder()).append(l).append("").toString();
        sqlitedatabase1.delete("image", "groupId=? and categroyId=?", as1);
    }

    public void closeDB() {
        db.close();
        repository = null;
    }

    public int delete(CategoryEntity categoryentity) {
        SQLiteDatabase sqlitedatabase = db;
        String as[] = new String[1];
        as[0] = String.valueOf(categoryentity.groupId);
        return sqlitedatabase.delete("collection", "groupId = ? ", as);
    }

    public int delete(ImageEntity imageentity) {
        SQLiteDatabase sqlitedatabase = db;
        String as[] = new String[1];
        as[0] = String.valueOf(imageentity.imageId);
        return sqlitedatabase.delete("image", "imageId = ? ", as);
    }

    public int delete(ModuleEntity moduleentity) {
        SQLiteDatabase sqlitedatabase = db;
        String as[] = new String[1];
        as[0] = String.valueOf(moduleentity.groupId);
        return sqlitedatabase.delete("module", "groupId = ? ", as);
    }

    public int isCategroyFaved(int i, long l) {
        Cursor cursor = null;
        int j = 0;
        try {
            cursor = null;
            SQLiteDatabase sqlitedatabase = db;
            String as[] = new String[2];
            as[0] = (new StringBuilder()).append(i).append("").toString();
            as[1] = (new StringBuilder()).append(l).append("").toString();
            cursor = sqlitedatabase.rawQuery("select isFav from collection where groupId=? and categroyId=?",
                    as);
            if (cursor.moveToFirst()) {
                int k = cursor.getInt(0);
                j = k;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return j;
    }

    public boolean isModuleExsit(int i) {
        Cursor cursor = null;
        boolean flag = false;
        try {
            SQLiteDatabase sqlitedatabase = db;
            String as[] = new String[1];
            as[0] = (new StringBuilder()).append(i).append("").toString();
            cursor = sqlitedatabase.rawQuery("select * from module where groupId=?", as);
            flag = cursor.moveToFirst();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return flag;
    }

    public ArrayList queryAllFavCategroys(long l) {
        ArrayList arraylist = new ArrayList();
        Cursor cursor;
        CategoryEntity categoryentity;
        if (l == 0L) {
            SQLiteDatabase sqlitedatabase1 = db;
            String as1[] = new String[1];
            as1[0] = "1";
            cursor = sqlitedatabase1.rawQuery(
                    "select * from collection where isFav=?  order by timestamp DESC limit 20", as1);
        } else {
            SQLiteDatabase sqlitedatabase = db;
            String as[] = new String[2];
            as[0] = "1";
            as[1] = (new StringBuilder()).append(l).append("").toString();
            cursor = sqlitedatabase
                    .rawQuery(
                            "select * from collection where isFav=? and timestamp<? order by timestamp DESC limit 20",
                            as);
        }
        for (; cursor.moveToNext(); arraylist.add(categoryentity)) {
            categoryentity = new CategoryEntity();
            categoryentity.groupId = cursor.getInt(cursor.getColumnIndex("groupId"));
            categoryentity.categroyId = cursor.getInt(cursor.getColumnIndex("categroyId"));
            categoryentity.description = cursor.getString(cursor.getColumnIndex("description"));
            categoryentity.iconUrl = cursor.getString(cursor.getColumnIndex("iconUrl"));
            categoryentity.timestamp = cursor.getInt(cursor.getColumnIndex("timestamp"));
            categoryentity.count = cursor.getInt(cursor.getColumnIndex("count"));
            categoryentity.isFav = cursor.getInt(cursor.getColumnIndex("isFav"));
        }

        cursor.close();
        return arraylist;
    }

    public ArrayList queryCategroys(int i, long l) {
        ArrayList arraylist = new ArrayList();
        Cursor cursor;
        CategoryEntity categoryentity;
        if (l == 0L) {
            SQLiteDatabase sqlitedatabase1 = db;
            String as1[] = new String[1];
            as1[0] = (new StringBuilder()).append(i).append("").toString();
            cursor = sqlitedatabase1.rawQuery(
                    "select * from collection where groupId=? order by timestamp DESC limit 20", as1);
        } else {
            SQLiteDatabase sqlitedatabase = db;
            String as[] = new String[2];
            as[0] = (new StringBuilder()).append(i).append("").toString();
            as[1] = (new StringBuilder()).append(l).append("").toString();
            cursor = sqlitedatabase
                    .rawQuery(
                            "select * from collection where groupId=? and timestamp<? order by timestamp DESC limit 20",
                            as);
        }
        for (; cursor.moveToNext(); arraylist.add(categoryentity)) {
            categoryentity = new CategoryEntity();
            categoryentity.groupId = cursor.getInt(cursor.getColumnIndex("groupId"));
            categoryentity.categroyId = cursor.getInt(cursor.getColumnIndex("categroyId"));
            categoryentity.description = cursor.getString(cursor.getColumnIndex("description"));
            categoryentity.iconUrl = cursor.getString(cursor.getColumnIndex("iconUrl"));
            categoryentity.timestamp = cursor.getInt(cursor.getColumnIndex("timestamp"));
            categoryentity.count = cursor.getInt(cursor.getColumnIndex("count"));
            categoryentity.isFav = cursor.getInt(cursor.getColumnIndex("isFav"));
        }

        cursor.close();
        return arraylist;
    }

    public ArrayList queryFavCategroys(int i, long l) {
        ArrayList arraylist = new ArrayList();
        Cursor cursor;
        CategoryEntity categoryentity;
        if (l == 0L) {
            SQLiteDatabase sqlitedatabase1 = db;
            String as1[] = new String[2];
            as1[0] = (new StringBuilder()).append(i).append("").toString();
            as1[1] = "1";
            cursor = sqlitedatabase1.rawQuery(
                    "select * from collection where groupId=? and isFav=? order by timestamp DESC limit 20",
                    as1);
        } else {
            SQLiteDatabase sqlitedatabase = db;
            String as[] = new String[3];
            as[0] = (new StringBuilder()).append(i).append("").toString();
            as[1] = "1";
            as[2] = (new StringBuilder()).append(l).append("").toString();
            cursor = sqlitedatabase
                    .rawQuery(
                            "select * from collection where groupId=? and isFav=? and timestamp<? order by timestamp DESC limit 20",
                            as);
        }
        for (; cursor.moveToNext(); arraylist.add(categoryentity)) {
            categoryentity = new CategoryEntity();
            categoryentity.groupId = cursor.getInt(cursor.getColumnIndex("groupId"));
            categoryentity.categroyId = cursor.getInt(cursor.getColumnIndex("categroyId"));
            categoryentity.description = cursor.getString(cursor.getColumnIndex("description"));
            categoryentity.iconUrl = cursor.getString(cursor.getColumnIndex("iconUrl"));
            categoryentity.timestamp = cursor.getInt(cursor.getColumnIndex("timestamp"));
            categoryentity.count = cursor.getInt(cursor.getColumnIndex("count"));
            categoryentity.isFav = cursor.getInt(cursor.getColumnIndex("isFav"));
        }

        cursor.close();
        return arraylist;
    }

    public ArrayList queryImages(int i, long l) {
        ArrayList arraylist = new ArrayList();
        SQLiteDatabase sqlitedatabase = db;
        String as[] = new String[2];
        as[0] = (new StringBuilder()).append(i).append("").toString();
        as[1] = (new StringBuilder()).append(l).append("").toString();
        Cursor cursor = sqlitedatabase.rawQuery(
                "select * from image where groupId=? and categroyId=? order by timestamp desc", as);
        for (int j = 0; cursor.moveToNext(); j++) {
            if (j != 0 && j % 4 == 0) {
                arraylist.add(new ImageEntity(-1, -1L));
                j++;
            }
            ImageEntity imageentity = new ImageEntity();
            imageentity.groupId = cursor.getInt(cursor.getColumnIndex("groupId"));
            imageentity.categroyId = cursor.getLong(cursor.getColumnIndex("categroyId"));
            imageentity.imageId = cursor.getLong(cursor.getColumnIndex("imageId"));
            imageentity.description = cursor.getString(cursor.getColumnIndex("description"));
            imageentity.imageUrl = cursor.getString(cursor.getColumnIndex("imageUrl"));
            imageentity.timestamp = cursor.getLong(cursor.getColumnIndex("timestamp"));
            arraylist.add(imageentity);
        }

        cursor.close();
        return arraylist;
    }

    public ArrayList queryModules() {
        ArrayList arraylist = new ArrayList();
        Cursor cursor;
        ModuleEntity moduleentity;
        for (cursor = db.rawQuery("select * from module ", null); cursor.moveToNext(); arraylist
                .add(moduleentity)) {
            moduleentity = new ModuleEntity();
            moduleentity.groupId = cursor.getInt(cursor.getColumnIndex("groupId"));
            moduleentity.moduleName = cursor.getString(cursor.getColumnIndex("moduleName"));
            moduleentity.iconUrl = cursor.getString(cursor.getColumnIndex("iconUrl"));
        }

        cursor.close();
        return arraylist;
    }
}
